package testebancodedados;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import javax.swing.*;

public class FilmeCRUDForm {
    private static FilmeCRUDControl control = new FilmeCRUDControl();
    private static JPanel pNome, pSaida;
    private static JTextField cod, nome, nomeSaida;
    private static JButton button;
    private static JComboBox op;
    
    public FilmeCRUDForm() {
    }
    
    public JPanel novoForm(){
        JPanel form = new JPanel();
        form.setLayout(new FlowLayout());
       
        JPanel panel = new JPanel(new GridLayout(6,1));
        form.add(panel);
      
        String[] array = new String[4];
        array[0] = "Cadastrar Filme";
        array[1] = "Remover Filme";
        array[2] = "Editar Filme";
        array[3] = "Visualizar Filme";
        JComboBox choice = new JComboBox(array);
        op = choice;
        panel.add(choice);
        ChoiceListener itemListener = new ChoiceListener() {};
        choice.addItemListener(itemListener);
      
        JPanel row = new JPanel();
        JLabel label = new JLabel("Código: "); // construct Label
        row.add(label);
        JTextField text = new JTextField("0000", 10);
        cod = text;
        row.add(text);
        panel.add(row);
      
        row = new JPanel();
        pNome = row;
        label = new JLabel("Nome: "); // construct Label
        row.add(label);
        text = new JTextField("Sem nome", 10);
        nome = text;
        row.add(text);
        panel.add(row);

        button = new JButton("Cadastrar Filme"); // construct Button
        panel.add(button);
        ButtonListener buttonListener = new ButtonListener();
        button.addActionListener(buttonListener);

        row = new JPanel();
        pSaida = row;
        text = new JTextField(10);
        text.setEditable(false);
        nomeSaida = text;
        row.add(text);
        panel.add(row);
        pSaida.setVisible(false);
        
        form.setSize(300, 300);
        form.setVisible(true);
        return form;
    }
    
    private static class ChoiceListener implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent ie) {
            button.setLabel(op.getSelectedItem().toString());
            switch (op.getSelectedIndex()) {
                case 0:
                    pNome.setVisible(true);
                    pSaida.setVisible(false);
                    break;
                case 1:
                    pNome.setVisible(false);
                    pSaida.setVisible(false);
                    break;
                case 2:
                    pNome.setVisible(true);
                    pSaida.setVisible(false);
                    break;
                case 3:
                    pNome.setVisible(false);
                    pSaida.setVisible(true);
                    break;
            }
        }
    }
    
    private static class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            String codigo = cod.getText();
            String n = nome.getText();
            
            switch (op.getSelectedIndex()) {
                case 0:
                    control.create(codigo, n);
                    break;
                case 1:
                    control.delete(codigo);
                    break;
                case 2:
                    control.update(codigo, n);
                    break;
                case 3:
                    ArrayList<String> rowList = control.read(codigo);
                    nomeSaida.setText(rowList.get(0));
                    break;
            }
        }
    }
   
}